package com.along.framework.utils;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsDateJsonBeanProcessor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 
 * @author Linliming
 * Date: 2010-4-16
 * Time: 16:50:23
 * 
 */
public class RspFuncs {

	private static JsonConfig jsonConfig;
	static {
		jsonConfig = new JsonConfig();
		JsDateJsonBeanProcessor beanProcessor = new JsDateJsonBeanProcessor();
		jsonConfig.registerJsonBeanProcessor(java.util.Date.class, beanProcessor);
	}

	public static void writeJson(HttpServletResponse response, Object obj) throws IOException {
		String json = JSONSerializer.toJSON(obj, jsonConfig).toString();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		setNoCache(response);
		response.getWriter().println(json);
	}

	/***
	 * @Description:
	 * @Author:Linliming
	 * @param response
	 * @param obj 输入的字符串
	 * @throws IOException
	 */
	public static void writeText(HttpServletResponse response, Object obj) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		setNoCache(response);
		response.getWriter().println(obj.toString());
	}

	public static void deleteCookie(HttpServletResponse response, String name, String path) {
		Cookie cookie = new Cookie(name, null);
		cookie.setPath("/");
		cookie.setMaxAge(0);
		response.addCookie(cookie);
	}
	public static void setNoCache(HttpServletResponse response) {
		response.setHeader("Cache-Control", "no-cache");
	}
}
